package com.yangzc.lion.sys.framework.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class ConfigLocationsUtil {

	private static String webContentPath;

	private static String[] enabledPath = null;

	private static String[] disabledPath = null;

	public static void setEnabledPath(String[] enabledPath) {
		ConfigLocationsUtil.enabledPath = enabledPath;
	}

	public static void setDisabledPath(String[] disabledPath) {
		ConfigLocationsUtil.disabledPath = disabledPath;
	}

	public static String getWebContentPath() {

		if (webContentPath == null) {
			URL url = null;
			Enumeration<URL> urls;
			try {
				urls = Thread.currentThread().getContextClassLoader()
						.getResources("/");
				while (urls.hasMoreElements()) {
					URL tmpUrl = urls.nextElement();
					String tmpPath = tmpUrl.getPath();
					if (!tmpPath.endsWith("/")) {
						tmpPath += "/";
					}
					if (tmpPath.endsWith("WEB-INF/classes/")) {
						url = tmpUrl;
						break;
					}
				}

				if (null == url) {
					url = Thread.currentThread().getContextClassLoader()
							.getResource("log4j.properties");
				}
			} catch (IOException e) {
				// e.printStackTrace();
			}

			webContentPath = url.getPath().substring(0,
					url.getPath().lastIndexOf("/WEB-INF/"));
			if (webContentPath.startsWith("file:")) {
				webContentPath = webContentPath.substring(5);
			}

			// 打出webContentPath信息,方便查错
			System.out.println("webContentPath:" + webContentPath + "\n");
		}
		return webContentPath;
	}

	public static String getKmssConfigPath() {
		return getWebContentPath() + "/WEB-INF/classes/config";
	}

	public static String[] getConfigLocationArray(String path,
			String cfgFileName) {
		return getConfigLocationArray(path, cfgFileName, "");
	}

	public static String[] getConfigLocationArray(String path,
			String cfgFileName, String filePrefix) {
		String curPath = "/WEB-INF/classes/config";
		File dir = new File(path + curPath);
		List<String> files = new ArrayList<String>();
		getConfigLocations(dir, curPath, cfgFileName, filePrefix, files);
		return (String[]) files.toArray(new String[0]);
	}

	public static String getConfigLocations(String path, String cfgFileName) {
		return getConfigLocations(path, cfgFileName, "");
	}

	/**
	 * @param path
	 *            样例：D:\tools\kmssworkplace\kmss_hr\WebContent
	 * @param cfgFileName
	 *            样例：spring.xml
	 * @param filePrefix
	 *            样例：file:///
	 * @return 
	 *         filePrefix+"/WEB-INF/KmssConfig/sys/common/spring.xml,"+filePrefix
	 *         +"/WEB-INF/KmssConfig/sys/notify/spring.xml"
	 */
	public static String getConfigLocations(String path, String cfgFileName,
			String filePrefix) {
		String[] files = getConfigLocationArray(path, cfgFileName, filePrefix);
		if (files.length == 0)
			return "";
		StringBuffer rtnVal = new StringBuffer();
		for (int i = 0; i < files.length; i++)
			rtnVal.append(',').append(files[i]);
		return rtnVal.substring(1);
	}

	public static List<File> getConfigLocationList(String path,
			String cfgFileName) {
		String curPath = "/WEB-INF/classes/config";
		File dir = new File(path + curPath);
		List<File> files = new ArrayList<File>();
		getConfigLocations(dir, cfgFileName, files);
		return files;
	}

	private static void getConfigLocations(File dir, String cfgFileName,
			List<File> files) {
		File[] fs = dir.listFiles();
		if (fs == null)
			return;
		for (File f : fs) {
			if (!f.isDirectory()) {
				if (f.getName().equals(cfgFileName)) {
					files.add(f);
				}
			} else {
				getConfigLocations(f, cfgFileName, files);
			}
		}
	}

	private static void getConfigLocations(File dir, String curPath,
			String cfgFileName, String filePrefix, List<String> files) {
		String[] fileNames = dir.list();
		if (fileNames == null)
			return;
		String filePath = dir.getPath();
		outloop: for (int i = 0; i < fileNames.length; i++) {
			if (fileNames[i].startsWith("."))
				continue;
			String newPath = curPath + "/" + fileNames[i];
			if (fileNames[i].equals(cfgFileName)) {
				if (enabledPath != null) {
					boolean found = false;
					for (int j = 0; j < enabledPath.length; j++) {
						if (newPath.startsWith("/WEB-INF/classes/config/"
								+ enabledPath[j])) {
							found = true;
							break;
						}
					}
					if (!found)
						continue;
				}
				if (disabledPath != null) {
					for (int j = 0; j < disabledPath.length; j++) {
						if (newPath.startsWith("/WEB-INF/classes/config/"
								+ disabledPath[j])) {
							continue outloop;
						}
					}
				}
				files.add(filePrefix + newPath);
				continue;
			}
			File file = new File(filePath + "/" + fileNames[i]);
			if (file.isDirectory()) {
				getConfigLocations(file, newPath, cfgFileName, filePrefix,
						files);
			}
		}
	}

}
